package ink.lovejinhu.leetcodeMatch;

import java.util.HashMap;
import java.util.HashSet;

public class Solution_5809 {
    /**
     * 返回长度为3的子序列
     *
     * @param s
     * @return
     */
    public int countPalindromicSubsequence(String s) {
        if (s.length() < 3) {
            return 0;
        }
        char[] chars = s.toCharArray();
        HashMap<Character, Integer> res0 = new HashMap<Character, Integer>(); //
        HashSet<String> haveFind = new HashSet<String>();
        int result = 0;
        for (int i = 0; i < chars.length; i++) {
            if (res0.containsKey(chars[i])) {  //
                Integer integer = res0.get(chars[i]); //上一次字符出现的位置
                int count = i - integer;
                if (count == 1) {
                    continue;
                }
                int v1 = 0;
                for (int j = integer + 1; j < i; j++) {
                    StringBuilder temp = new StringBuilder();
                    temp.append(chars[i]);
                    temp.append(chars[j]);
                    temp.append(chars[i]);
                    String s1 = temp.toString();
                    if (haveFind.contains(s1)) {
                        continue;
                    } else {
                        v1++;
                    }
                }
                result = result + v1;
                res0.put(chars[i],i);
            } else {
                res0.put(chars[i], i);

            }

        }
        return result;
    }

    public static void main(String[] args) {
        Solution_5809 solution_5809 = new Solution_5809();
        String s = "aabca";
        solution_5809.countPalindromicSubsequence(s);
        System.out.println(solution_5809.countPalindromicSubsequence(s));


    }

}

